/*******************************************************************************
 * Package: com.song.boot.springstudy.webflux
 * Type:    ErrorMain
 * Date:    2025/4/7 9:31
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.webflux;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;

/**
 * 功能描述：处理流中的异常。
 *
 * @author Songxianyang
 * @date 2025/4/7 9:31
 */
public class ErrorMain {
    public static void main(String[] args) {
        //onErrorResume：发生错误时切换到一个备用流。
        Flux.just(1, 2, 3).flatMap(s -> Mono.error(new Exception("ex")))
                .onErrorResume(e -> Flux.just(33, 44))
                .subscribe(System.out::println);
        // onErrorReturn：发生错误时返回默认值。
        Flux.just(1, 2, 3).flatMap(s -> Mono.error(new Exception("ex")))
                .onErrorReturn("默认值")
                .subscribe(System.out::println);

        // 重试
        Flux.error(new Exception("ex"))
                .retry(3)
                // .doOnNext(s -> System.out.println(s))
                .subscribe(System.out::println);
    }
}
